<?php
require dirname(__FILE__).'/function.php';
$dplayer = new DPlayer_class();
RegisterPlugin("DPlayer", "ActivePlugin_DPlayer");

function ActivePlugin_DPlayer() {
	Add_Filter_Plugin('Filter_Plugin_ViewPost_Template', 'DPlayer_Filter_Plugin_ViewPost_Template');
	Add_Filter_Plugin('Filter_Plugin_ViewList_Template', 'DPlayer_Filter_Plugin_ViewList_Template');
	Add_Filter_Plugin('Filter_Plugin_Zbp_MakeTemplatetags', 'DPlayer_Filter_Plugin_Zbp_MakeTemplatetags');
}

function DPlayer_Filter_Plugin_ViewPost_Template(&$template) {
    global $zbp;
    global $dplayer;
	$article = $template->GetTags('article');
	$article->Content = $dplayer->parseCallback($article->Content, $zbp->Config('DPlayer'));
}

function DPlayer_Filter_Plugin_ViewList_Template(&$template) {
    global $zbp;
    global $dplayer;
	$config = $zbp->Config('DPlayer');
	if ($config->parselist) {
	    $articles = $template->GetTags('articles');
	    foreach($articles as $article) $article->Intro = $dplayer->parseCallback($article->Intro, $config);
	}
}

function DPlayer_Filter_Plugin_Zbp_MakeTemplatetags() {
    global $zbp;
    if ($zbp->Config('DPlayer')->flv) $zbp->footer .= '<script type="text/javascript" src="'.$zbp->host.'zb_users/plugin/DPlayer/plugin/flv.min.js"></script>'."\n";
    if ($zbp->Config('DPlayer')->hls) $zbp->footer .= '<script type="text/javascript" src="'.$zbp->host.'zb_users/plugin/DPlayer/plugin/hls.min.js"></script>'."\n";
    $zbp->footer .=
    '<script type="text/javascript" src="'.$zbp->host.'zb_users/plugin/DPlayer/DPlayer.min.js?v=1.1.3"></script>'."\n".
    '<script>function dpajaxload(){if(0<$(\'#dpajax\').length){var DPlayerOptions=[];eval($(\'#dpajax\').text());for(i=0;i<DPlayerOptions.length;i++)new DPlayer({element:document.getElementById(\'dp\'+DPlayerOptions[i].id),autoplay:DPlayerOptions[i].autoplay,theme:DPlayerOptions[i].theme,loop:DPlayerOptions[i].loop,lang:DPlayerOptions[i].lang,screenshot:DPlayerOptions[i].screenshot,hotkey:DPlayerOptions[i].hotkey,preload:DPlayerOptions[i].preload,video:DPlayerOptions[i].video,danmaku:DPlayerOptions[i].danmaku})}}dpajaxload();</script>';
}

function InstallPlugin_DPlayer() {
	global $zbp,$obj,$bucket;
    if (!$zbp->Config('DPlayer')->HasKey('theme')) {
        $zbp->Config('DPlayer')->siteurl = $zbp->host;
        $zbp->Config('DPlayer')->dmserver = '//api.prprpr.me/dplayer/';
        $zbp->Config('DPlayer')->useue = 1;
		$zbp->Config('DPlayer')->hidermmenu = 0;
		$zbp->Config('DPlayer')->hotkey = 1;
		$zbp->Config('DPlayer')->danmaku = 1;
		$zbp->Config('DPlayer')->screenshot = 0;
		$zbp->Config('DPlayer')->loop = 0;
		$zbp->Config('DPlayer')->autoplay = 0;
		$zbp->Config('DPlayer')->preload = 0;
		$zbp->Config('DPlayer')->lang = 1;
		$zbp->Config('DPlayer')->maximum = 1000;
		$zbp->Config('DPlayer')->flv = 1;
		$zbp->Config('DPlayer')->hls = 0;
		$zbp->Config('DPlayer')->theme = '#FADFA3';
		$zbp->Config('DPlayer')->parselist = 0;
        $zbp->SaveConfig('DPlayer');
    }
}

function UninstallPlugin_DPlayer() {
	global $zbp;
	if ($zbp->Config('DPlayer')->hidermmenu == '1') {
	    $dpjs = file_get_contents(dirname(__FILE__)."/DPlayer.min.js");
		$dpjs = str_replace('<!--<div class="dplayer-menu">', '<div class="dplayer-menu">', $dpjs);
		$dpjs = str_replace('About DPlayer")+"</a></span></div>\n            </div>-->\n', 'About DPlayer")+"</a></span></div>\n            </div>\n', $dpjs);
		file_put_contents(dirname(__FILE__)."/DPlayer.min.js", $dpjs);
	}
	$zbp->DelConfig('DPlayer');
}